package Offer;

/**
 * @author lakersUp
 * @Classname  CountDigitOne
 * @Description  1-n整数中1出现的次数
 *           将数字划分为三部分: 高位,当前位,低位.  根据当前位curr值得不同分为三种情况(0,1,其他)
 *         每一种情况的流程    1.出现1的数字范围
 *                              2. 只看高低位
 *                              3. 易得1的个数
 * @Date 2022/2/26 16:16
 * @Version 1.0
 */
public class Off43countDigitOne {
    public int countDigitOne(int n) {
        int high=n;
        int low=0;
        int curr=0;
        int num=1;
        int count=0;
        while(high!=0 || curr!=0){
            curr=high%10;
            high/=10;
            if(curr==0){
                count+=high*num;
            }else {
                if(curr==1){
                    count+=high*num+low+1;
                }else {
                    count+=(high+1)*num;
                }
            }
            low=curr*num+low;
            num*=10;
        }
        return count;
    }
}
